package com.iwm.qa.mdm.adapter;

import android.app.Activity;
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.iwm.qa.mdm.R;
import com.iwm.qa.mdm.model.Borrow;
import com.iwm.qa.mdm.view.RecyclerViewFragment;

import java.util.LinkedHashMap;
import java.util.List;

/**
 * Created by zhaohaiyang on 2018/02/11.
 */

public class BorrowRecyclerAdapter extends RecyclerView.Adapter<BorrowRecyclerAdapter.BorrowViewHolder> {

    private Activity mActivity;
    private Fragment mFragment;
    private Borrow borrowBean;
    LinkedHashMap<String, String> linkedHashMap = new LinkedHashMap<String, String>();

    public BorrowRecyclerAdapter(Activity activity) {
        this.mActivity = activity;
    }

    public BorrowRecyclerAdapter(RecyclerViewFragment recyclerViewFragment) {
        this.mFragment = recyclerViewFragment;
    }

    public void setData(Borrow borrowBean) {
        this.borrowBean = borrowBean;
        notifyDataSetChanged();
    }

    @Override
    public BorrowViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View v = LayoutInflater.from(viewGroup.getContext())
                .inflate(R.layout.borrow_card_item, viewGroup, false);
        return new BorrowViewHolder(v);
    }

    @Override
    public void onBindViewHolder(BorrowViewHolder viewHolder, int position) {
        List<Borrow.DataBean> data = borrowBean.getData();
        Borrow.DataBean dataBean = data.get(position);
        if (data != null && data.size() > 0) {
            viewHolder.mDeviceName.setText(dataBean.getDeviceName());
            viewHolder.mDeviceName.setText(dataBean.getDeviceDept());
            viewHolder.mBorrowTime.setText(dataBean.getBorrowTime());
            viewHolder.mDevicePhoto.setImageDrawable(ContextCompat.getDrawable(mActivity,R.drawable.github));
            //将position保存在itemView的Tag中，以便点击时进行获取
            viewHolder.itemView.setOnClickListener(mClickListener);
//            viewHolder.itemView.setOnLongClickListener(mLongClickListener);
            viewHolder.itemView.setTag(dataBean);
        }
    }

    @Override
    public int getItemCount() {
        if (borrowBean == null || borrowBean.getData() == null) {
            return 0;
        }
        return borrowBean.getData().size();
    }

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
    }

    private View.OnClickListener mClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Borrow.DataBean dataBean = (Borrow.DataBean) view.getTag();

            Intent intent = new Intent(mActivity, RecyclerViewFragment.class);
            intent.putExtra("DEVICE_NAME", dataBean.getDeviceName());
            intent.putExtra("BORROW_PERSON", dataBean.getBorrowPerson());
            intent.putExtra("BORROW_TIME", dataBean.getBorrowTime());
            intent.putExtra("OS_TYPE", dataBean.getOsType());
            intent.putExtra("VERSION_TYPE", dataBean.getVersionType());
            intent.putExtra("DEVICE_RESOLUTION", dataBean.getDeviceResolution());
            intent.putExtra("DEVICE_DEPT", dataBean.getDeviceDept());
            intent.putExtra("REMARK", dataBean.getRemark());
            mActivity.startActivity(intent);
        }
    };

    public static class BorrowViewHolder extends RecyclerView.ViewHolder {

        CardView cv;
        TextView mDeviceName;
        TextView mBorrowPerson;
        TextView mBorrowTime;
        ImageView mDevicePhoto;


        BorrowViewHolder(View itemView) {
            super(itemView);
            cv = (CardView) itemView.findViewById(R.id.cv);
            mDeviceName = (TextView) itemView.findViewById(R.id.device_name);
            mBorrowPerson = (TextView) itemView.findViewById(R.id.borrow_person);
            mBorrowTime = (TextView) itemView.findViewById(R.id.borrow_time);
            mDevicePhoto = (ImageView) itemView.findViewById(R.id.device_photo);
        }
    }
}
